package com.wanpinghui.supplier.message;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;

import com.malinskiy.superrecyclerview.OnMoreListener;
import com.malinskiy.superrecyclerview.SuperRecyclerView;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.message.adapter.BidSuccMsgAdapter;
import com.wanpinghui.supplier.object.ObjectDetailActivity;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.message.MessageService;
import com.wanpinghui.wphlibrary.model.message.bean.MsgBean;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import retrofit.callback.YRequestCallback;

/***
 * 功能描述:竞价成功通知
 * 作者:qiujialiu
 * 时间:2017/12/8
 ***/
@ContentView(R.layout.layout_activity_bid_success_notification)
public class BidSucNotificationActivity extends BaseActivity {
    @ViewInject(R.id.recv_message_list)
    private SuperRecyclerView mRecyclerView;
    @ViewInject(R.id.view_backtrack)
    private LinearLayout mViewBack;

    private BidSuccMsgAdapter mAdapter;
    private String groupId;
    private long createAt;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        if (getIntent() != null) {
            groupId = getIntent().getStringExtra("groupId");
        }
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new BidSuccMsgAdapter(this);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void bindEvents() {
        mViewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mAdapter.setClickListener(new BidSuccMsgAdapter.ClickListener() {
            @Override
            public void goDetail(long demandId) {
                Intent intent = new Intent(BidSucNotificationActivity.this, ObjectDetailActivity.class);
                intent.putExtra("id", demandId);
                intent.putExtra("notification", true);
                startActivity(intent);
            }

            @Override
            public void callPhone(String phone) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        WPHSdk.getService(MessageService.class)
                .getMsgHistory(groupId, createAt, new YRequestCallback<List<MsgBean>>() {
                    @Override
                    public void onSuccess(List<MsgBean> var1) {
                        mRecyclerView.setRefreshing(false);
                        if (var1 != null && var1.size() > 0) {
                            if (createAt == 0) {
                                mAdapter.setDatas(var1);
                            } else {
                                mAdapter.addDatas(var1);
                            }
                            createAt = var1.get(var1.size() - 1).getCreated_at();
                        }
                        if (var1 != null && var1.size() < 20) {
                            mRecyclerView.removeMoreListener();
                            mRecyclerView.hideMoreProgress();
                        } else {
                            mRecyclerView.setupMoreListener(new OnMoreListener() {
                                @Override
                                public void onMoreAsked(int overallItemsCount, int itemsBeforeMore, int maxLastVisiblePosition) {
                                    initData();
                                }
                            }, 1);
                        }


                    }

                    @Override
                    public void onFailed(int var1, String message) {
                        mRecyclerView.setRefreshing(false);
                        showWarningText(message);
                    }

                    @Override
                    public void onException(Throwable var1) {
                        mRecyclerView.setRefreshing(false);
                        var1.printStackTrace();
                    }
                });
    }
}
